package com.yupi.springbootinit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.datasource.PictureDatasource;
import com.yupi.springbootinit.datasource.PostDatasource;
import com.yupi.springbootinit.datasource.SearchDatasource;
import com.yupi.springbootinit.datasource.UserDatasource;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.search.SearchRequestDTO;
import com.yupi.springbootinit.model.enums.SearchTypeEnum;
import com.yupi.springbootinit.model.vo.SearchVO;
import com.yupi.springbootinit.service.PictureService;
import com.yupi.springbootinit.service.PostService;
import com.yupi.springbootinit.service.SearchService;
import com.yupi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
@Slf4j
public class SearchServiceImpl  implements SearchService {

    @Resource
    private PostDatasource postDatasource;

    @Resource
    private PictureDatasource pictureDatasource;

    @Resource
    private UserDatasource userDatasource;
    @Resource
    private PostService postService;

    @Resource
    private PictureService pictureService;

    @Resource
    private UserService userService;
    @Override
    public SearchVO searchAll(SearchRequestDTO searchRequestDTO, int pageNum, int pageSize) {
        String type = searchRequestDTO.getType();
        ThrowUtils.throwIf(StrUtil.isBlank(type), ErrorCode.PARAMS_ERROR);
        SearchTypeEnum typeEnum = SearchTypeEnum.getEnumByValue(type);
//        Page<Picture> pictureReturnPage=null;
//        Page<PostVO> postReturnPage=null;
//        Page<UserVO> userVOReturnPage=null;
//        if(typeEnum== ALL){
//            //       post
//            CompletableFuture<Page<PostVO>> postTask = new CompletableFuture<>().supplyAsync(() -> {
//                Page<PostVO> postPage = postDatasource.search(searchRequestDTO.getSearchText(), pageNum, pageSize);
//                return postPage;
//            });
////        picture
//            CompletableFuture<Page<Picture>> pictureTask = new CompletableFuture<>().supplyAsync(() -> {
//                Page<Picture> picturePage = pictureDatasource.search(searchRequestDTO.getSearchText(), pageNum, pageSize);
//                return picturePage;
//            });
////         user
//            CompletableFuture<Page<UserVO>> userTask = new CompletableFuture<>().supplyAsync(() -> {
//                Page<UserVO> userVOPage = userDatasource.search(searchRequestDTO.getSearchText(), pageNum, pageSize);
//                return userVOPage;
//            });
////    等待所有任务执行完才会执行
//            CompletableFuture.allOf(postTask,pictureTask,userTask).join();
//
//            try {
//                pictureReturnPage = pictureTask.get();
//                postReturnPage = postTask.get();
//                userVOReturnPage = userTask.get();
//            } catch (Exception e) {
//                log.error("聚合接口异常");
//                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"接口异常");
//            }
//        }else{
            HashMap<String, SearchDatasource<T>> map =new HashMap(){{
                put(SearchTypeEnum.POST.getValue(),postDatasource);
                put(SearchTypeEnum.PICTURE.getValue(),pictureDatasource);
                put(SearchTypeEnum.USER.getValue(),userDatasource);
            }};
            SearchDatasource datasource = map.get(type);
            Page searchResult = datasource.search(searchRequestDTO.getSearchText(), pageNum, pageSize);

//        }
//         整合返回值
        SearchVO searchVO = new SearchVO();
        searchVO.setDataList(searchResult);
        System.out.println(searchVO);
        return searchVO;
    }
}
